package com.example.signin.ui.home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.signin.bean.PersonBean
import com.example.signin.databinding.FragmentHomeBinding
import com.example.signin.uitl.HttpUtil
import com.google.gson.Gson
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private val homeViewModel: HomeViewModel
        get() {
            return ViewModelProvider(this).get(HomeViewModel::class.java)
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val content =
            "{\"code\": 0, \"message\": \"\\u8bf7\\u6c42\\u6210\\u529f\", \"data\": [{\"id\": 1, \"name\": \"\\u90b5\\u660e\\u5e86\", \"isArrive\": 0, \"department\": \"\\u7814\\u53d1\\u4e00\\u90e8\", \"updatedTime\": \"2019-11-06 09:21:59\", \"samplePhoto\": \"https://www.keaidian.com/uploads/allimg/190424/24110307_6.jpg\", \"createdTime\": \"2019-11-06 09:21:59\"}, {\"id\": 2, \"name\": \"\\u738b\\u96ea\\u742e\", \"isArrive\": 0, \"department\": \"\\u7814\\u53d1\\u4e8c\\u90e8\", \"updatedTime\": \"2019-11-06 09:22:02\", \"samplePhoto\": \"https://www.keaidian.com/uploads/allimg/190424/24110307_8.jpg\", \"createdTime\": \"2019-11-06 09:22:02\"}, {\"id\": 3, \"name\": \"\\u5f20\\u4e09\", \"isArrive\": 1, \"department\": \"\\u7814\\u53d1\\u4e00\\u90e8\", \"updatedTime\": \"2020-10-13 09:26:23\", \"samplePhoto\": \"https://www.keaidian.com/uploads/allimg/190424/24110307_8.jpg\", \"createdTime\": \"2020-10-13 09:26:23\"}, {\"id\": 4, \"name\": \"\\u674e\\u56db\", \"isArrive\": 1, \"department\": \"\\u7814\\u53d1\\u4e00\\u90e8\", \"updatedTime\": \"2020-11-01 12:20:53\", \"samplePhoto\": \"https://www.keaidian.com/uploads/allimg/190424/24110307_8.jpg\", \"createdTime\": \"2020-11-01 12:20:53\"}, {\"id\": 5, \"name\": \"\\u738b\\u4e94\", \"isArrive\": 1, \"department\": \"\\u7814\\u53d1\\u4e00\\u90e8\", \"updatedTime\": \"2021-05-06 12:30:45\", \"samplePhoto\": \"https://www.keaidian.com/uploads/allimg/190424/24110307_8.jpg\", \"createdTime\": \"2021-05-06 12:30:45\"}, {\"id\": 6, \"name\": \"\\u5218\\u516d\", \"isArrive\": 1, \"department\": \"\\u7814\\u53d1\\u4e8c\\u90e8\", \"updatedTime\": \"2021-12-02 11:12:23\", \"samplePhoto\": \"https://www.keaidian.com/uploads/allimg/190424/24110307_8.jpg\", \"createdTime\": \"2021-12-02 11:12:23\"}]}"
        val bean = Gson().fromJson(content, PersonBean::class.java)
        binding.recyclerview.layoutManager =
            LinearLayoutManager(requireContext()).apply {
                orientation = LinearLayoutManager.VERTICAL
            }
        homeViewModel.bindAdapter(binding.recyclerview, bean)
        initStatistics(bean)
    }

    private fun initData() {
        HttpUtil.httpGet(
            "http://192.168.1.6:5000/getPersonnelList",
            HashMap(),
            object : HttpUtil.CallBack {
                override fun success(result: HttpUtil.Result?) {
                    Log.d("TAG", "success: ${result?.content}")
                    result?.let {
                        if (it.success) {
                            val bean = Gson().fromJson(it.content, PersonBean::class.java)
                            binding.recyclerview.layoutManager =
                                LinearLayoutManager(requireContext()).apply {
                                    orientation = LinearLayoutManager.VERTICAL
                                }
                            homeViewModel.bindAdapter(binding.recyclerview, bean)
                        }
                    }
                }

                override fun failed(e: IOException?) {
                    e?.printStackTrace()
                }

            })
    }

    private fun initStatistics(personBean: PersonBean) {
        var arrive = 0f
        var notArrive = 0f
        var later = 0f
        personBean.data.forEach {
            if (it.isArrive == 0) {
                notArrive += 1f
            } else {
                val time =
                    (SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(it.updatedTime)
                        ?: Date(System.currentTimeMillis() - 10)).time
                val c = Calendar.getInstance()
                c[Calendar.HOUR_OF_DAY] = 9
                c[Calendar.MINUTE] = 0
                c[Calendar.SECOND] = 0
                val m6 = c.time.time
                if (time <= m6) {
                    arrive += 1f
                } else {
                    later += 1f
                }
            }
        }
        binding.checked.text = String.format(Locale.CHINA, "%.0f人", arrive)
        binding.unchecked.text = String.format(Locale.CHINA, "%.0f人", notArrive)
        binding.late.text = String.format(Locale.CHINA, "%.0f人", later)
        val a = arrive / (arrive + notArrive) * 100
        binding.attendance.text = String.format(Locale.CHINA, "%.2f%%", a)

        binding.myStatisticsView.setData(a)
        binding.myStatisticsView3.setData((100f - a))
        val late = later / (arrive + notArrive) * 100
        binding.myStatisticsView2.setData(late)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}